package com.nlabs.slickgwt.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
 


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */

public class SlickGWT implements EntryPoint {
	 
	 private PopupPanel msg_popup = new PopupPanel(); 
	 private HTML msg = new HTML(); 
	 
	public void onModuleLoad() {
		
		 RootPanel root = RootPanel.get("app");
		 
		 msg_popup.setStylePrimaryName("msg-popup");
		 msg_popup.setAnimationEnabled(true);
		 msg_popup.setWidget(msg);
		 
		 
		 VerticalPanel main_panel = new VerticalPanel()  ;
		 main_panel.setSize("100%", "150");
		 main_panel.setSpacing(5);
		 
		 VerticalPanel button_panel = createDemoSection("Buttons");
		 
		 HorizontalPanel buttons = new HorizontalPanel();
		 buttons.setSpacing(10);
		 SlickButton b1 = new SlickButton("Click Me");
		 b1.setTitle("SlickButton");
		 b1.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				 msg.setHTML("SlickButton Clicked");
				 msg_popup.center(); 
				 Timer t = new Timer() {

					@Override
					public void run() {
						msg_popup.hide(); 
						
					}
				 
				 };
				 
				 t.schedule(2000);
			}
			 
			 
		 });
		 
		 buttons.add(b1);
		 ImageButton b2 = new ImageButton("resources/checkmark.png", "Click Me");
		 b2.setTitle("ImageButton");
		 b2.addClickHandler(new ClickHandler() {

				public void onClick(ClickEvent event) {
					 msg.setHTML("ImageButton Clicked");
					 msg_popup.center(); 
					 Timer t = new Timer() {

						@Override
						public void run() {
							msg_popup.hide(); 
							
						}
					 
					 };
					 
					 t.schedule(2000);
				}
				 
				 
			 });
		 buttons.add(b2);
		 
		 
		 IconButton b3 = new IconButton("resources/checkmark.png");
		 b3.setTitle("IconButton");
		 b3.addClickHandler(new ClickHandler() {

				public void onClick(ClickEvent event) {
					 msg.setHTML("IconButton Clicked");
					 msg_popup.center(); 
					 Timer t = new Timer() {

						@Override
						public void run() {
							msg_popup.hide(); 
							
						}
					 
					 };
					 
					 t.schedule(2000);
				}
				 
				 
			 });
		 buttons.add(b3);
		 
		 MenuButton b4 = new MenuButton("File");
		 MenuItem item1 = new MenuItem("New");
		 MenuItem item2 = new MenuItem("Open...");
		 MenuItem item3 = new MenuItem("Save");
		 b4.addMenuItem(item1);
		 b4.addMenuItem(item2);
		 b4.addMenuItem(item3);
		 
		 b4.setTitle("MenuButton");
		  
		 buttons.add(b4);
		 
		 
		 button_panel.add(buttons);
		 
		 main_panel.add(button_panel);
		 
		 VerticalPanel window_panel = createDemoSection("Window");
		 
		 final Window awindow = new Window(); 
		 
		 VerticalPanel panel = new VerticalPanel();
		 panel.setSize("300", "200");
		 SlickButton close = new SlickButton("Close");
		 close.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				awindow.close();
				
			}
		 });
		 panel.setSpacing(5);
		 panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
		 panel.add(close);
		 panel.setCellHorizontalAlignment(close, HasHorizontalAlignment.ALIGN_RIGHT);
		 
		 
		 awindow.setHeading("A Window");
		 awindow.setWidget(panel);
		 
		 SlickButton b5 = new SlickButton("Click Me");
		 b5.addClickHandler(new ClickHandler() {

				public void onClick(ClickEvent event) {
					 awindow.center(); 
					  
				}
				 
				 
			 });
		 
		 b5.setTitle("Click to show Window");
		 window_panel.add(b5);
		 
		 main_panel.add(window_panel);
		 
		 VerticalPanel layouts_panel = createDemoSection("Layouts");
		  
		 final Window awindow2 = new Window(); 
		 
		 BorderLayoutPanel blpanel = new BorderLayoutPanel();
		 
		 blpanel.addNorthWidget(createLabeledPanel("North"), 50);
		 blpanel.addWestWidget(createLabeledPanel("West"), 50);
		 blpanel.addEastWidget(createLabeledPanel("East"), 50);
		 blpanel.addSouthWidget(createLabeledPanel("South"), 50);
		 blpanel.addCenterWidget(createLabeledPanel("Center"));
		 
		 blpanel.setSize("400", "300");
		 
		 awindow2.setHeading("Border Layout");
		 awindow2.setWidget(blpanel);
		 
		 SlickButton b6 = new SlickButton("Border Layout");
		 b6.setSize("85", "40");
		 b6.addClickHandler(new ClickHandler() {

				public void onClick(ClickEvent event) {
					 awindow2.center(); 
					  
				}
				 
				 
			 });
		 
		 b6.setTitle("Click to show BorderLayout");
		 layouts_panel.add(b6);
		 
		 main_panel.add(layouts_panel);
		 
		 root.add(main_panel);
		 
		 
	}
	
	public HorizontalPanel createLabeledPanel(String label){
		
		HorizontalPanel panel = new HorizontalPanel();
		panel.setStylePrimaryName("borderlayout-container");
		panel.setSpacing(2);
		panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		HTML html = new HTML(label);
		panel.add(html);
		panel.setCellVerticalAlignment(html, HasVerticalAlignment.ALIGN_MIDDLE);
		return panel; 
		
	}
	
	public VerticalPanel createDemoSection(String name) {
		
		 VerticalPanel panel = new VerticalPanel(); 
		  panel.setSpacing(5);
		  panel.setWidth("100%");
		  panel.setStylePrimaryName("widget-container");
		  panel.add(new HTML("<h2>" + name + "</h2>"));
		  return panel; 
		
		
	}
}
